package com.ihcy.base.model.entity;

import com.ihcy.base.enums.PositioneEnum;
import com.ihcy.base.enums.convert.PositioneEnumJpaConvert;
import com.ihcy.base.model.vo.request.RegisterUserRequest;
import com.ihcy.base.model.vo.request.UserQueryRequest;
import lombok.*;
import org.hibernate.annotations.Type;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Entity
@EntityListeners(AuditingEntityListener.class) // 配合 @CreatedDate  @LastModifiedDate
@Table(name = "t_user")
@org.hibernate.annotations.Table(appliesTo = "t_user", comment = "用户表")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(nullable = false, unique = true, columnDefinition = "varchar(10) comment '员工编号'")
    private String employeeNo;

    private String userName;

    private String phone;

    private String cardNo;

//    @Enumerated(EnumType.STRING)
    @Convert(converter = PositioneEnumJpaConvert.class)
    @Column(nullable = false)
    private PositioneEnum position;

    @Column
    @Type(type = "yes_no")
    private Boolean isDeleted;
    @CreatedDate
    @Column(updatable = false)
    private LocalDateTime createdTime;
    @LastModifiedDate
    private LocalDateTime updatedTime;

    public static User build(RegisterUserRequest request) {
        return User.builder()
                .employeeNo(request.getEmployeeNo())
                .userName(request.getUserName())
                .phone(request.getPhone())
                .cardNo(request.getCardNo())
                .position(request.getPosition())
                .isDeleted(Boolean.FALSE)
                .build();
    }

    public static List<User> build(List<RegisterUserRequest> list) {
        return list.stream().map(User::build).collect(Collectors.toList());
    }

    public static User build(UserQueryRequest request) {
        return User.builder()
                .userName(request.getUserName())
                .build();
    }
}
